from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装盘点搜索函数
def inventory_search(search_data):
    # 跳转至盘点列表界面
    browser.find_element(By.ID, "mainmenutd_products").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_titre>a[title='盘点']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_product_inventory_list .vsmenu").click()

    # 编号
    browser.find_element(By.NAME, "search_ref").send_keys(search_data["ref"])
    # 标签
    browser.find_element(By.NAME, "search_title").send_keys(search_data["title"])
    # 仓库
    browser.find_element(By.CSS_SELECTOR, f"select#search_fk_warehouse>option:nth-child({search_data['fk_warehouse']})").click()
    # 产品
    browser.find_element(By.CSS_SELECTOR, f"select#search_fk_product>option:nth-child({search_data['fk_product']})").click()
    # 状态
    browser.find_element(By.CSS_SELECTOR, f"select#search_status>option:nth-child({search_data['status']})").click()
    # 搜索按钮
    browser.find_element(By.NAME, "button_search_x").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 盘点搜索功能测试数据（3正向+3反向，共6个用例）
        # 测试结果：与预期结果完全一致
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "case_desc": "搜索存在的标准仓库盘点单",
                "ref": "INV-20250615-001",  # 存在的盘点单编号
                "title": "原材料仓库盘点-202506",
                "fk_warehouse": 2,  # 原材料仓
                "fk_product": 3,  # 第三个产品
                "status": 2,  # 生效状态
                "expected_result": "搜索结果显示1条记录，与输入条件匹配"
            },
            {
                "case_desc": "搜索存在的成品仓库盘点单",
                "ref": "INV-20250615-002",  # 存在的盘点单编号
                "title": "成品仓库盘点-2025年6月",
                "fk_warehouse": 3,  # 成品仓
                "fk_product": 5,  # 第五个产品
                "status": 2,
                "expected_result": "搜索结果显示1条记录，与输入条件匹配"
            },
            {
                "case_desc": "搜索存在的配件仓库盘点单",
                "ref": "INV-20250615-003",  # 存在的盘点单编号
                "title": "配件仓库月度盘点",
                "fk_warehouse": 4,  # 配件仓
                "fk_product": 2,  # 第二个产品
                "status": 2,
                "expected_result": "搜索结果显示1条记录，与输入条件匹配"
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "case_desc": "搜索不存在的盘点单",
                "ref": "INV-20250615-999",  # 不存在的编号
                "title": "",
                "fk_warehouse": 2,
                "fk_product": 2,
                "status": 2,
                "expected_result": "搜索结果显示0条记录，提示'未找到匹配数据'"
            },
            {
                "case_desc": "空编号搜索",
                "ref": "",  # 空编号
                "title": "原材料仓库盘点-202506",
                "fk_warehouse": 2,
                "fk_product": 3,
                "status": 2,
                "expected_result": "搜索结果显示所有符合仓库和产品条件的盘点单"
            },
            {
                "case_desc": "错误格式编号搜索",
                "ref": "INVALID-REF",  # 错误格式编号
                "title": "",
                "fk_warehouse": 2,
                "fk_product": 2,
                "status": 2,
                "expected_result": "搜索结果显示0条记录，提示'未找到匹配数据'"
            }
        ]
        for data in test_data:
            inventory_search(data)
            sleep(1)  # 等待搜索结果加载
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()